<template>
  <div class="app-container">
  <el-row>
        <el-col :span="4" style="margin-left: -24px;margin-top:-12px" >
          <FileTree @getNode="getNode" ref="fileTree" :key="itemFileTree" :fileType="fileType"></FileTree>
        </el-col>
    <el-col :span="20">
      <el-form :model="queryParams"  style="margin-left:20px" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="文件柜名称" prop="cabinetName">
        <el-input
          v-model="queryParams.cabinetName"
          placeholder="请输入文件柜名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="文件柜位置" prop="cabinetLocation">
        <el-input
          v-model="queryParams.cabinetLocation"
          placeholder="请输入文件柜位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>   -->
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
      </el-form>

          <el-row :gutter="10" class="mb8" >
            <el-col :span="1.5">
              <el-button type="primary" style="margin-left:30px" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" ></right-toolbar>
          </el-row>
      <div style="width: 100%;height: 90vh;overflow:auto; margin-bottom: 30%;padding-bottom: 18%;position: relative;">
          <el-table style="margin-left:30px" height="407px" :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }" v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="序号" align="center" type="index" width="70px" :index="indexMethod" :show-overflow-tooltip="true"/>
            <el-table-column label="排序" align="center" prop="orderNum" width="70px"/>
            <el-table-column label="文件柜名称" align="center" prop="cabinetName" width="400px" :show-overflow-tooltip="true"/>
            <el-table-column label="上级文件柜" align="center" width="300px" prop="superiorCabinetName" :show-overflow-tooltip="true"/>
            <el-table-column label="文件柜位置" align="center" prop="cabinetLocation" width="100px" />

            <el-table-column label="操作"  align="center" width="100px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <!-- <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleqx(scope.row)"
                 >权限设置</el-button> -->
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" title="修改"
                 ></el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" title="删除"
                  ></el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination style="margin-left:30px" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
        </el-col>
      </el-row>
    <!-- 添加或修改文件柜维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-form-item label="显示排序" prop="orderNum">
              <el-input-number style="width: 280px" v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="文件柜名称" prop="cabinetName">
          <el-input v-model="form.cabinetName" placeholder="请输入文件柜名称" maxlength="50" />
        </el-form-item>
        <!-- <el-form-item label="文件柜位置" prop="cabinetLocation" placeholder="请选择文件柜位置">
          <el-select v-model="form.cabinetLocation"  @change="cityChange">
            <el-option v-for="dict in dict.type.file_cabinet_location" :key="dict.value"
                :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="上级文件柜" prop="superiorCabinetId">
          <treeselect v-model="form.superiorCabinetId" :default-expand-level="2" :options="optionData" :normalizer="normalizer" :flat="true"
            :show-count="true" placeholder="请选择上级文件柜" style="width: 465.5px"  :disabled="showLable" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 权限设置对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="qxopen" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-form-item label="文件柜名称" prop="cabinetName">
          <el-input v-model="form.cabinetName" placeholder="请输入文件柜名称" disabled="true"/>
        </el-form-item>
        <el-form-item label="读写权限" prop="readWritePermission">
          <el-input v-model="form.readWritePermission" type="textarea" placeholder="请选择" rows="5" style="width:330px" disabled="true"/>
          <el-button type="primary" @click="selectOpen(2)" style="margin:0px 10px 80px 10px;">选 择</el-button>
          <el-button @click="canceldx" style="margin:-100px 0px 0px 340px;">清 空</el-button>
        </el-form-item>

        <el-form-item label="只读权限" prop="readOnlyPermission">
          <el-input v-model="form.readOnlyPermission" type="textarea" placeholder="请选择" rows="5" style="width:330px" disabled="true"/>
          <el-button type="primary" @click="selectOpen(1)" style="margin:0px 10px 80px 10px;">选 择</el-button>
          <el-button @click="cancelzd" style="margin:-100px 0px 0px 340px;">清 空</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormqx">确 定</el-button>
        <el-button @click="cancelqx">关 闭</el-button>
      </div>
    </el-dialog>
    <select-user ref="select" :sign="sign" :fjgFiName="fjgFiName" :wName="wName" :wNameZD="wNameZD" :showType="show" @selectUser="selectUser"></select-user> -->
  </div>
</template>

<script>
import {listExternal} from "@/api/file/external";
import { listFileTree,listInformation, getInformation, delInformation, addInformation, updateInformation } from "@/api/file/information";
import FileTree from "@/components/File/index";
import { listInternal} from "@/api/file/internal";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SelectUser from "../../system/role/selectUserNew.vue";
export default {
  name: "Information",
  dicts: ['file_cabinet_location'],
  components: { FileTree,Treeselect,SelectUser },
  data() {
    return {
      showLable:false,
      fjgFiName:"",
      wName:"",
      wNameZD:"",
      formClass: {},
      fileType:'1',
      sign:'1',
      itemFileTree:0,
      // item: {
      //     ['value':1,'label':'内部'],
      //     ['value':2,'label':'外部'],
      // },
      optionData: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      show: '',
      allTbale:null,
      // 总条数
      total: 0,
      // 文件柜维护表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      qxopen:false,
      cabinetId:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        cabinetId: null,
        cabinetName: null,
        cabinetLocation: null,
        superiorCabinetId: null,
        superiorCabinetName: null,
        readWritePermissionId: null,
        readWritePermission: null,
        readOnlyPermissionId: null,
        readOnlyPermission: null,
        creationTime: null,
        operateTime: null,
        operateId: null,
        departmentId: null,
        departmentName: null,
        operateName: null,
        organizationId: null,
        organizationName: null,
        creationId: null,
        creationName: null,
        belongTo: null,
        idPath: null,

      },
      queryParamsInternal:{
        pageNum: 1,
        pageSize: 15,
        cabinetId: null,
      },
      queryParamsExternal:{
        pageNum: 1,
        pageSize: 15,
        cabinetId: null,
      },
      num:0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // readOnlyPermission: [
        //   { required: true, message: "只读权限不能为空", trigger: "change" }
        // ],
        // readWritePermission: [
        //   { required: true, message: "读写权限不能为空", trigger: "change" }
        // ],
        superiorCabinetId: [
          { required: true, message: "上级文件柜不能为空", trigger: "change" }
        ],
        cabinetName: [
          { required: true, message: "文件柜名称不能为空", trigger: "blur" },
          { max: 50, message: '文件柜名称长度不能超过50', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.listFileTree();
    //this.refTbale();
  },
  methods: {
    refTbale(){
      this.$nextTick(()=>{
        this.allTbale=true
      })


    },
      //序号展示
    indexMethod(index){
      return (this.queryParams.pageNum-1)*this.queryParams.pageSize +index+1;
    },
    // //选择用户
    // selectUser(userIds, userNames) {
    //   if (this.show == '2') {
    //     this.$set(this.form, 'readWritePermission', userNames.join(','))//读写
    //     this.$set(this.form, 'readWritePermissionId', userIds.join(','))

    //   } else if (this.show == '1') {
    //     this.$set(this.form, 'readOnlyPermission', userNames.join(','))//只读
    //     this.$set(this.form, 'readOnlyPermissionId', userIds.join(','))

    //   }
    // },
    //加载文件柜树
    listFileTree() {
      listFileTree().then(res => {
        this.optionData = res.data;
        this.$nextTick(()=>{
          this.allTbale=true
        })
      })
    },
    cityChange(val) {
      //alert("选中城市id:", val)

    },
    /** 查询文件柜维护列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // // 取消按钮
    // cancelqx() {
    //   this.qxopen = false;
    //   this.reset();
    // },
    // 表单重置
    reset() {
      this.form = {
        cabinetId: null,
        cabinetName: null,
        cabinetLocation: null,
        superiorCabinetId: null,
        superiorCabinetName: null,
        readWritePermissionId: null,
        readWritePermission: null,
        readOnlyPermissionId: null,
        readOnlyPermission: null,
        creationTime: null,
        operateTime: null,
        operateId: null,
        orderNum:null,
        departmentId: null,
        departmentName: null,
        operateName: null,
        organizationId: null,
        organizationName: null,
        creationId: null,
        creationName: null,
        belongTo: null,
        idPath: null
      };
      this.resetForm("form");
    },
    //禁用父节点
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        isDisabled: node.parentId == '00000000-0000-0000-0000-000000000000' ? true : false,
        children: node.children

      }
    },
    // //用户选择
    // selectOpen(type) {
    //   this.wName = this.form.readWritePermissionId;//读写
    //   this.wNameZD = this.form.readOnlyPermissionId;//只读
    //   // setTimeout(() => {

    //   // },100)

    //   this.show = type
    //   this.$refs.select.show();

    // },
    //树点击事件
    getNode(data, node, obj) {
      this.reset();
      if(data.id=='947cc995-011c-4577-81cc-8478ce3bab4b'){
        this.queryParams.idPath = null
      }else{
        this.cabinetId = data.id
        this.queryParams.idPath = data.id
      }

      this.getList()
    },
     /** 新增按钮操作 */
     handleAdd() {
      this.reset();
      this.showLable=false;
      this.listFileTree()
      this.open = true;
      if(this.cabinetId!=""){
        this.$set(this.form, 'superiorCabinetId', this.cabinetId)
      }
      this.title = "添加文件柜";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // //清空 只读
    // cancelzd() {
    //   this.form.readOnlyPermission="";
    //   this.form.readOnlyPermissionId="";
    //   //this.wNameZD = this.form.readOnlyPermission;
    // },

     //清空 读写
     canceldx() {
      this.form.readWritePermission="";
      this.form.readWritePermissionId="";
      //this.wName = this.form.readWritePermission;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cabinetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 权限设置按钮操作 */
    handleqx(row) {
      this.reset();
      const cabinetId = row.cabinetId || this.ids
      getInformation(cabinetId).then(response => {
        this.fjgFiName = cabinetId;
        this.form = response.data;
        this.qxopen = true;
        this.title = "权限设置";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.listFileTree()
      this.queryParams.superiorCabinetId = row.cabinetId
      this.queryParamsInternal.cabinetId = row.cabinetId
      this.queryParamsExternal.cabinetId = row.cabinetId

      listInformation(this.queryParams).then(response => {
        if(response.total>0){
            this.showLable=true;
        }else{
          if(row.cabinetLocation=='外部'){
            listExternal(this.queryParamsExternal).then(response => {
              if(response.total>0){
                this.showLable=true;
              }
            });
          }else{
            listInternal(this.queryParamsInternal).then(response => {
              if(response.total>0){
                this.showLable=true;
              }
            });
          }
        }
      });

      const cabinetId = row.cabinetId || this.ids
      getInformation(cabinetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件柜";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.cabinetId != null){
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.queryParams.superiorCabinetId = null
              this.getList();
              this.itemFileTree++;
            });
          }else{
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.queryParams.superiorCabinetId = null
              this.getList();
              this.itemFileTree++;
            });
          }

        }
      });
    },
    /** 权限设置提交按钮 */
    submitFormqx() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.qxopen = false;
              this.getList();
              this.itemFileTree++;
          });

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cabinetIds = row.cabinetId || this.ids;
      this.$modal.confirm('是否确认删除文件柜名称为"' + row.cabinetName + '"的数据项？').then(function() {
        return delInformation(cabinetIds)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
        this.itemFileTree++;
        this.$nextTick(() => {
          this.$refs.fileTree.getList();
        })

      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/information/export', {
        ...this.queryParams
      }, `information_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>

</style>

